package com.prevpn.controller;

import com.prevpn.common.CommonResponse;
import com.prevpn.dto.AssignRoleByNameDTO;
import com.prevpn.dto.UserCreateDTO;
import com.prevpn.service.UserService;
import com.prevpn.vo.UserDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增用户
     * POST /user/create
     */
    @PostMapping("/create")
    public CommonResponse<Map<String, Object>> createUser(@RequestBody UserCreateDTO dto) {
        return userService.createUser(dto);
    }

    /**
     * 根据邮箱查询用户详细信息
     */
    @GetMapping("/queryByEmail")
    public CommonResponse<UserDetailVO> queryUserByEmail(
            @RequestParam String directoryDomain,
            @RequestParam String email
    ) {
        return userService.queryUserByEmail(directoryDomain, email);
    }

    /**
     * 用户分页查询接口（支持 recursive / fuzzyMatch / searchByPath）
     */
    @PostMapping("/queryAll")
    public CommonResponse<Map<String, Object>> queryAllUsers(@RequestBody Map<String, Object> body) {
        return userService.queryUserList(body);
    }

    /**
     * 根据用户名查询用户详情
     * GET /user/queryByName?directoryDomain=local&name=zhangsan
     */
    @GetMapping("/queryByName")
    public CommonResponse<UserDetailVO> queryUserByName(
            @RequestParam String directoryDomain,
            @RequestParam String name
    ) {
        return userService.queryUserByName(directoryDomain, name);
    }

    /**
     * 用户关联角色（基于用户名）
     * POST /user/assignRoleByName
     */
    @PostMapping("/assignRoleByName")
    public CommonResponse<Map<String, Object>> assignRoleByName(@RequestBody AssignRoleByNameDTO dto) {
        return userService.assignRoleByName(dto);
    }

    /**
     * 批量删除用户 - 基于名称
     * @param directoryDomain 用户目录（如 local）
     * @param nameList 用户名数组
     */
    @PostMapping("/deleteByNameList")
    public CommonResponse<Object> bulkDeleteUserByNameList(
            @RequestParam String directoryDomain,
            @RequestParam List<String> nameList) {

        return userService.bulkDeleteUserByNameList(directoryDomain, nameList);
    }

    /**
     * 编辑用户 - 基于名称
     * POST /user/updateByName
     * directoryDomain 和 name 是必须的，其他参数根据需要填写
     */
    @PostMapping("/updateByName")
    public CommonResponse<Object> updateUserByName(@RequestBody Map<String, Object> body) {
        return userService.updateUserByName(body);
    }
}
